﻿<?php
	
	require_once '../dbconfig.php';

	if($_POST)
	{
		$user_name = $_POST['user_name'];
		$password = $_POST['password'];
		$gender = $_POST['gender'];
		
		try
		{	
		
			$repeatQuery = $db_con->prepare("SELECT * FROM users WHERE usr_name=:name");
			$repeatQuery->execute(array(":name"=>$user_name));
			$count = $repeatQuery->rowCount();
			
			if($count==0){
				
			$repeatQuery = $db_con->prepare("INSERT INTO users(usr_name,usr_pw,usr_gender) VALUES(:uname, :pass, :gender)");
			$repeatQuery->bindParam(":uname",$user_name);
			$repeatQuery->bindParam(":pass",$password);
			$repeatQuery->bindParam(":gender",$gender);
					
				if($repeatQuery->execute())
				{
					$getIdSql =  $db_con->prepare("SELECT * FROM users WHERE usr_name=:name");
					$getIdSql->execute(array(":name"=>$user_name));
					$rst = $getIdSql->fetch(PDO::FETCH_ASSOC);
					$usr_id = $rst['usr_id'];
					$initEquipments = $db_con->prepare("INSERT INTO equipments(usr_id,eqp_mag) VALUES (:uid,-1)");
					if($initEquipments->execute(array(":uid"=>$usr_id))){
						echo "success";
					}else{
						echo "initEquiFail";
					}
					
				}
				else
				{
					echo "unknown";
				}
			
			}
			else{
				
				echo "registered";
			}
				
		}
		catch(PDOException $e){
			echo $e->getMessage();
		}
	}

?>